import { useRequest } from 'umi';
import { DBZ_STATE, WJBZZ_STATE } from '../common/Format';
import { useCallback, useState, useMemo } from 'react';
import { requestFunComponFiex } from '@/utils';
import { DocumentationManageTab } from '../mconfig/ManageTabSwitch';
import { searchCriteriaFun } from '../../Acommon';
import { getBaseUrl } from '@/services/urlconst';
const baseUrl = getBaseUrl();

export const DocumentationManageService = (curRes) => {
  /***保存当前的搜索参数**/
  const [manageTableParams, setManageTableParams] = useState<any>({});

  /** ManageServices */
  const request = useRequest(
    (requestParams: any) => {
      let params: any = {
        current: 1,
        pageSize: 10,
        state: `${DBZ_STATE()},${WJBZZ_STATE()}`,
        ...requestParams,
        ...DocumentationManageTab(curRes),
        order: 'desc',
        sort: 'createTime',
      };

      const reqUrl = `${baseUrl}/purchaseSourceController/dataGrid/${params?.params}`;
      /***保存一份参数**/
      setManageTableParams(params);
      return requestFunComponFiex('POST', reqUrl, params);
    },
    {
      manual: false,
      paginated: true,
      defaultPageSize: 10,
    },
  );

  const reloadRequest = useCallback((reqParams: any) => {
    let reqObj = searchCriteriaFun(reqParams);
    request?.run(reqObj);
  }, []);

  const dataSource = useMemo(() => {
    let data = request.data?.rows;
    if (data && data?.length) {
      return data;
    } else {
      return [];
    }
  }, [request.data?.rows]);

  return {
    getDocumentationRun: request?.run,
    getDocumentationLoading: request?.loading,
    reloadRequest,
    dataSource,
    manageTableParams,
    ManageTableProps: request.tableProps,
    reloadRequests: request?.refresh,
  };
};
